package com.controller;

import com.api.CommonResult;
import com.entity.BGoods;
import com.entity.table.BGoodsTableDef;
import com.entity.table.HomeTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Home;
import com.service.HomeService;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author admin
 * @since 2024-06-23
 */
@Controller
@RequestMapping("/admin/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    //添加获取商品列表代码
    @GetMapping(value = "/home")
    public String goodList(Model model, @RequestParam(value = "homename", defaultValue = "") String homename,
                           @RequestParam(value = "p",defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //按照用户名模糊查找
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(HomeTableDef.HOME.HOMENAME.like(homename));
            Page<Home> page = new Page<>();
            page.setPageSize(pageSize); //设置每页显示记录数
            page.setPageNumber(p);  //设置页码
            Page<Home> pageInfo = homeService.getMapper().paginateWithRelations(page, queryWrapper);
            model.addAttribute("page", pageInfo);
            model.addAttribute("total",pageInfo.getTotalRow());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "back/home-list";
    }

    @GetMapping(value = "/addView")
    public String addView() {return "back/home-add";}

    /**
     * 修改添加用户表代码。
     *
     * @param home 用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Home home){
       try {
           boolean r = homeService.save(home);
           return CommonResult.success(r);
       }catch (Exception e) {
           e.printStackTrace();
           String msg = home.getHomename()+"已存在";
           return CommonResult.failed(msg);
       }
    }

    //添加修改
    @GetMapping(value = "updateView")
    public String updateView(int id, Model model){
        Home home = homeService.getById(id);
        model.addAttribute("home", home);
        return "back/home-edit";
    }

    /**
     * 修改根据主键更新用户表代码。
     *
     * @param home 用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Home home){
        try {
            boolean r = homeService.updateById(home);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = home.getHomename()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    //添加删除
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try {
            boolean r = homeService.removeById(id);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}
